<!--
 * @Description: 质量问题描述
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2022-08-19 15:04:01
 * @LastEditTime: 2022-08-20 15:17:42
 * @FilePath: \cscec-pms-admin\src\components\Select\SelectProblemTypeTree.vue
-->
<template>
  <div>
    <a-tree-select
      v-model="currentValue"
      v-bind="$attrs"
      :tree-data="treeData"
      :replace-fields="{ children: 'chlildrenList', title: 'name', key: 'id', value: 'id' }"
      :show-checked-strategy="SHOW_CHILD"
      :placeholder="placeholder"
      tree-default-expand-all
      allow-clear
      v-on="$listeners"
    >
     <a-icon v-if="fetching" style="color: #096dd9" slot="suffixIcon" type="loading" />
    </a-tree-select>
  </div>
</template>
<script>
import { problemGetTree } from '@/api/quality/quality' // 问题维护api
import { TreeSelect } from 'ant-design-vue'
const SHOW_CHILD = TreeSelect.SHOW_CHILD
export default {
  name: 'SelectProblemTypeTree',
  components: {},
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Object]
    },
    // placeholder
    placeholder: {
      type: String,
      default: function () {
        return '请选择'
      }
    },
    // 分部分项id
    subProjectId: {     
      type: [String, Number],
      default: function () {
        return ''
      }
    },
    // 检查类别id 必传
    checkTypeId: {
      type: [String, Number],
      default: function () {
        return ''
      }
    },
    // 工程类别id 必传
    projectTypeId: {
      type: [String, Number],
      default: function () {
        return ''
      }
    },
    isDisabled: {
      type: Boolean,
      default: () => false
    },
    isManagement:{  //是否是管理行为
      type: Boolean,
      default: () => false
    },
    isFlagcheckType:{  //是否是工程实体和工程措施
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
       fetching: false,
      treeData: [],
      SHOW_CHILD
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        let value = this.value
        return value
      },
      set: function (value) {
        // this.$emit('update:labels', value.map((i) => i.label)?.join('，'))
        // value = value.map((i) => i.value)
        console.log(value)
        this.$emit('input', value)
      }
    }
  },
  watch: {
    subProjectId: {
      handler(val) {
        if(!this.isManagement){
            this.treeData = [];
        }
        if (this.projectTypeId && this.checkTypeId) {
           this.getDate()
        }
      },
      deep: true
    },
     checkTypeId: {
      handler(val) {
         this.treeData = [];
        if (val  && this.projectTypeId || val && this.isManagement) {

          this.getDate()
        }
      },
      deep: true
    },
     projectTypeId: {
      handler(val) {
        if(!this.isManagement){
            this.treeData = [];
        }
        if (val  && this.checkTypeId) {
          this.getDate()
        }
      },
      deep: true
    }
  },
  mounted() {
    if (this.checkTypeId && this.projectTypeId) {
      this.getDate()
    }
  },
  methods: {
    deepTree(list) {
      list.forEach((item) => {
        if (Array.isArray(item.chlildrenList) && item.chlildrenList.length > 0) {
          this.deepTree(item.chlildrenList)
        } else {
          if (Array.isArray(item.problemTypeListList) && item.chlildrenList.length > 0) {
            this.deepTree(item.problemTypeListList)
          }
        }
      })
    },
    /**
     * @description: 获取树
     * @return {void}
     */
    getDate() {
        let obj = {
        }
        if(this.subProjectId){
           obj.id = this.subProjectId
        }
        if(this.checkTypeId){
           obj.checkTypeId = this.checkTypeId
        }
          if(this.projectTypeId){
           obj.projectTypeId = this.projectTypeId
        }
        if(!this.isFlagcheckType){   //工程实体和工程措施，三个参数必须在，才能请求
           if(!obj.id || !obj.checkTypeId || !obj.projectTypeId){
            return 
           }
        }
        this.fetching = true
      problemGetTree.getList(obj).then((res) => {
           this.treeData = [];
           this.fetching = false
        if (res.code === this.SUCCESS_CODE) {
          this.treeData = res.data?.chlildrenList
          this.isDisabled && this.isDisabledFn(this.treeData )
        }
      })
    },
    isDisabledFn(data){
      return data && data.map((item)=>{
           if(Array.isArray(item.chlildrenList) && item.chlildrenList.length > 0){
            item.disabled = !item.isSelect;  
            return  this.isDisabledFn(item.chlildrenList)
           }else{
        
           }
        })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
